package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.LinkFormDataEditGetDTO;
import com.xbongbong.paas.pojo.dto.LinkFormDataFastAddGetDTO;
import com.xbongbong.paas.pojo.vo.LinkFormDataAddGetVO;
import com.xbongbong.paas.pojo.vo.LinkFormDataEditGetVO;
import com.xbongbong.paas.service.PaasLinkFormDataService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.invoice.pojo.dto.InvoiceLinkContractGetDTO;
import com.xbongbong.pro.invoice.pojo.vo.InvoiceLinkContractGetVO;
import com.xbongbong.pro.payment.pojo.dto.PaymentWriteOffGetDTO;
import com.xbongbong.pro.payment.pojo.vo.PaymentWriteOffGetVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 应用模块名称<p>
 * 代码描述<p>
 * Company: 逍邦网络科技有限公司<p>
 *
 * @author chenshan
 * @version v1.0
 * @since 2020-02-13 14:17
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/linkForm/data")
public class MobilePaasLinkFormDataController {
    @Resource
    private PaasLinkFormDataService paasLinkFormDataService;
    
    @RequestMapping(value = "/add/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addGet(@RequestBody @Valid LinkFormDataFastAddGetDTO linkFormDataFastAddGetDTO, BindingResult br) {
        XbbResponse<LinkFormDataAddGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LinkFormDataAddGetVO linkFormDataAddGetVO = paasLinkFormDataService.linkFastAddGet(linkFormDataFastAddGetDTO);
                response = new XbbResponse<>(linkFormDataAddGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    
    @RequestMapping(value = "/edit/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String editGet(@RequestBody @Valid LinkFormDataEditGetDTO linkFormDataEditGetDTO, BindingResult br) {
        XbbResponse<LinkFormDataEditGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LinkFormDataEditGetVO linkFormDataEditGetVO = paasLinkFormDataService.linkFastEditGet(linkFormDataEditGetDTO);
                response = new XbbResponse<>(linkFormDataEditGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 点击应收款核销页面渲染
     * @param paymentWriteOffGetDTO
     * @param br
     * @return java.lang.String
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/payment/writeOff/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String paymentWriteOffGet(@RequestBody @Valid PaymentWriteOffGetDTO paymentWriteOffGetDTO, BindingResult br) {
        XbbResponse<PaymentWriteOffGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PaymentWriteOffGetVO paymentWriteOffGetVO = paasLinkFormDataService.paymentWriteOffGet(paymentWriteOffGetDTO);
                response = new XbbResponse<>(paymentWriteOffGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * 点击销项发票关联合同按钮，界面渲染
     * @param invoiceLinkContractGetDTO
     * @param br
     * @return
     * @author lvruijuan
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/invoice/contract/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String invoiceLinkContractGet(@RequestBody @Valid InvoiceLinkContractGetDTO invoiceLinkContractGetDTO, BindingResult br) {
        XbbResponse<InvoiceLinkContractGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                InvoiceLinkContractGetVO invoiceLinkContractGetVO = paasLinkFormDataService.invoiceLinkContractGet(invoiceLinkContractGetDTO);
                response = new XbbResponse<>(invoiceLinkContractGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


}
